package cn.com.surker.material.controller;

import cn.com.surker.common.AbstractController;
import cn.com.surker.common.BaseGroupValidated;
import cn.com.surker.dto.main.MainMaterialBillAddDTO;
import cn.com.surker.dto.main.MainMaterialBillAuditDTO;
import cn.com.surker.dto.main.MainMaterialBillDTO;
import cn.com.surker.material.service.MaterialBillBaseService;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.entity.SysReportTime;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.SysReportTimeService;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author tf
 * @create 2024年12月5日21:47:16
 * @desc
 */
@RestController
@RequestMapping("/main-material-bill")
public class MainMaterialBillController extends AbstractController {


    @Resource
    private MaterialBillBaseService materialBillBaseService;

    @Resource
    private SysReportTimeService sysReportTimeService;


    /**
     * 主材材料结算信息表 分页方法
     * @param dto
     * @return
     */
    @PostMapping(value = "/page")
    public RestResultDto pageData(@RequestBody MainMaterialBillDTO dto){
        dto.setType(4);
        CacheUser user = getUser();
        return RestResultDto.success(materialBillBaseService.page(dto));
    }

    /**
     * 主材材料结算信息表新增方法
     * @param dto
     * @return
     */
    @PostMapping(value = "/save")
    public RestResultDto save(@Validated(BaseGroupValidated.AddGroup.class) @RequestBody MainMaterialBillAddDTO dto){
        dto.setType(4);
        CacheUser user = getUser();
        return RestResultDto.success(materialBillBaseService.saveBill(dto,user));
    }

    /**
     * 主材材料结算信息表更新方法
     * @param dto
     * @return
     */
    @PostMapping(value = "/update")
    public RestResultDto update(@Validated(BaseGroupValidated.UpdateGroup.class)@RequestBody MainMaterialBillAddDTO dto){
        CacheUser user = getUser();
        return RestResultDto.success(materialBillBaseService.updateBill(dto,user));
    }

    /**
     * 主材材料结算信息表更新方法
     * @param dto
     * @return
     */
    @PostMapping(value = "/updatestatus")
    public RestResultDto updatestatus(@Validated(BaseGroupValidated.UpdateStatusGroup.class)@RequestBody MainMaterialBillAddDTO dto){
        CacheUser user = getUser();
        return RestResultDto.success(materialBillBaseService.updateStatus(dto,user));
    }

    /**
     * 审核方法
     * @param dto
     * @return
     */
    @PostMapping(value = "/auditdata")
    public RestResultDto auditdata(@Validated(BaseGroupValidated.AuditGroup.class)@RequestBody MainMaterialBillAuditDTO dto){
        CacheUser user = getUser();
        return RestResultDto.success(materialBillBaseService.auditdata(dto,user));
    }

    /**
     * 获取单位关联的供应商信息
     * @param dto
     * @return
     */
    @PostMapping(value = "/getSupplierList")
    public RestResultDto getSupplierList(@RequestBody MainMaterialBillAddDTO dto){
        if(ObjectUtil.isEmpty(dto.getOrgId())){
            RestResultDto.fail("公司标识获取失败！");
        }
        List<Map<String, Object>> supplierListByOrgId = materialBillBaseService.getSupplierList(Long.parseLong(dto.getOrgId()+""));
        return RestResultDto.success(supplierListByOrgId);
    }

    /**
     * 获取单位关联的材料类型信息
     * @param dto
     * @return
     */
    @PostMapping(value = "/getMaterialList")
    public RestResultDto getMaterialList(@RequestBody MainMaterialBillAddDTO dto){
        if(ObjectUtil.isEmpty(dto.getOrgId())){
            RestResultDto.fail("公司标识获取失败！");
        }
        List<Map<String, Object>> materialListByOrgId = materialBillBaseService.getMaterialList(Long.parseLong(dto.getOrgId()+""));
        return RestResultDto.success(materialListByOrgId);
    }

    /**
     * 获取报表开始时间
     * @param
     * @return
     */
    @GetMapping(value = "/getToDayList")
    public RestResultDto getToDayList(){
        List<SysReportTime> getToDayList = sysReportTimeService.findTodayAll();
        return RestResultDto.success(getToDayList);
    }


}
